package com.cskaoyan.market.admin.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.cskaoyan.market.db.domain.MarketBrand;
import com.cskaoyan.market.db.domain.MarketBrandExample;
import com.cskaoyan.market.db.mapper.MarketBrandMapper;
import com.cskaoyan.market.admin.service.MarketBrandService;
import com.cskaoyan.market.util.MyBatisUtil;
import com.github.pagehelper.PageHelper;
import org.apache.ibatis.session.SqlSession;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @NAME: AdminBrandServiceImpl
 * @DESCRIPTION:
 * @DATE: 2023/5/8 10:08
 * @AUTHOR: 刘超
 */
@Service
public class MarketBrandServiceImpl implements MarketBrandService {
    @Autowired
    private MarketBrandMapper mapper;

    @Override
    public List<MarketBrand> list(String page, String limit, String sort, String order) {

        
        

        MarketBrandExample brandExample = new MarketBrandExample();
        brandExample.orderBy(sort + " " + order).or().andDeletedEqualTo(false);
        PageHelper.startPage(Integer.parseInt(page), Integer.parseInt(limit));
        List<MarketBrand> brands = mapper.selectByExample(brandExample);
        
        
        return brands;
    }

    @Override
    public List<MarketBrand> list(Integer id) {
        
        

        MarketBrandExample brandExample = new MarketBrandExample();
        brandExample.or().andIdEqualTo(id).andDeletedEqualTo(false);;
        List<MarketBrand> brands = mapper.selectByExample(brandExample);
        
        
        return brands;

    }
    @Override
    public List<MarketBrand> list(String page, String limit, String sort, String order,String name) {
        
        

        MarketBrandExample brandExample = new MarketBrandExample();
        brandExample.orderBy(sort + " " + order).or().andNameLike("%" + name + "%").andDeletedEqualTo(false);
        PageHelper.startPage(Integer.parseInt(page), Integer.parseInt(limit));
        List<MarketBrand> brands = mapper.selectByExample(brandExample);
        
        
        return brands;

    }

    @Override
    public MarketBrand create(MarketBrand marketBrand) {
        
        
        mapper.insert(marketBrand);

        MarketBrandExample brandExample = new MarketBrandExample();
        
        brandExample.or().andNameEqualTo(marketBrand.getName());
        return mapper.selectOneByExample(brandExample);
    }

    @Override
    public MarketBrand update(MarketBrand marketBrand) {
        
        
        mapper.updateByPrimaryKey(marketBrand);
        mapper.selectByPrimaryKey(marketBrand.getId());

        
        
        return marketBrand;
    }

    @Override
    public int delete(String id) {
        
        
        MarketBrand marketBrand = mapper.selectByPrimaryKey(Integer.parseInt(id));
        marketBrand.setUpdateTime(LocalDateTime.now());
        marketBrand.setDeleted(true);
        mapper.updateByPrimaryKey(marketBrand);
        
        
        return 0;
    }
}
